package com.itheima.controller.sms;



import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.sms.SmsHomeBrand;
import com.itheima.pojo.sms.vo.UpdateVo;
import com.itheima.service.sms.SmsHomeBrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("home/brand")
public class SmsHomeBrandController {

    @DubboReference
    private SmsHomeBrandService brandService;

    /**
     * 添加首页推荐品牌
     * @param homeBrandList
     * @return
     */
    @PostMapping("create")
    public R create(@RequestBody List<SmsHomeBrand> homeBrandList) {
        for (SmsHomeBrand smsHomeBrand : homeBrandList) {
            brandService.save(smsHomeBrand);
        }

        return R.success("添加成功");
    }

    /**
     * 批量删除推荐品牌
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public R delete(@RequestParam("ids") List<Long> ids) {
        brandService.removeByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 批量修改推荐品牌状态
     * @param
     * @return
     */
    @PostMapping("update/recommendStatus")
    public R updateRecommendStatus(@RequestParam ("ids")List<Long>ids, @RequestParam Integer recommendStatus) {
        brandService.updateRecommendStatus(ids,recommendStatus);
        return R.success("修改成功");
    }

    /**
     * 修改推荐品牌排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("update/sort/{id}")
    public R updateSort(@PathVariable("id") Long id, @RequestParam("sort") Integer sort) {

        SmsHomeBrand brand = new SmsHomeBrand();
        brand.setId(id);
        brand.setSort(sort);
        brandService.updateById(brand);
        return R.success("修改成功");
    }

    /**
     * 分页查询推荐品牌
     * @param pageNum
     * @param pageSize
     * @param brandName
     * @param recommendStatus
     * @return
     */
    @GetMapping("list")
    public R list(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam(value = "brandName",required = false)String brandName,@RequestParam(value = "recommendStatus",required = false)Integer recommendStatus){
        PageInfo commonPage = brandService.listPage(pageNum,pageSize,brandName,recommendStatus);
        return R.success(commonPage);
    }

}
